﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HiStaff.Util;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.iSystem;
using HiStaff.Domain.CO;
using HiStaff.Util.Enum;
using HiStaff.Domain.Sys;
namespace HiStaff.iTime
{
    public partial class SignSelectionDisplay : FormBase
    {
        public DialogResult SelectionResult { get; set; }
        public List<CO_SIGN_LST> SelectionList { get; set; }
        public string FormName { get; set; }

        public SignSelectionDisplay()
        {
            InitializeComponent();
        }

        protected override bool FormLoad()
        {
            try
            {
                CO_SIGN_LST objSelection;
                SelectionResult = DialogResult.Cancel;
                SelectionList = new List<CO_SIGN_LST>();

                base.CreateCommandBar(CommandBarManager.ActionCommand.Save,
                                      CommandBarManager.ActionCommand.Cancel,
                                      CommandBarManager.ActionCommand.Separator,
                                      CommandBarManager.ActionCommand.Exit);

                List<SY_DEFAULT_LIST> lstSource;

                lstSource = ConvertHelper.DataTableToList<SY_DEFAULT_LIST>(HiStaff.iSystem.Data.DATALIST.Tables["COSIGNGROUP"]);
                grdData.RootTable.Columns["GROUPID"].ValueList.PopulateValueList(lstSource, "ID", "NAME");

                var lstSign = (from obj in HiStaff.iSystem.Data.ALLCOSIGN
                               where obj.STATUS == 1
                               select obj).ToList();
                grdData.SetDataBinding(lstSign, "ID");

                List<TIME_CFN> lstCfn = TimeUtil.GetSupportDisplayColumn(FormName);
                if (lstCfn != null && lstCfn.Count > 0)
                {
                    foreach (var grData in grdData.GetDataRows())
                    {
                        foreach (var iCfn in lstCfn)
                        {
                            if (iCfn.Id == ((CO_SIGN_LST)grData.DataRow).CODE)
                            {
                                grData.CheckState = Janus.Windows.GridEX.RowCheckState.Checked;
                                objSelection = ((CO_SIGN_LST)grData.DataRow);
                                objSelection.IDX = iCfn.Windex;
                                objSelection.FORMATSTRING = iCfn.FormatString;
                            }
                        }
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
                HiStaff.Util.Log.Instance.WriteExceptionLog(ex, this.Name + "[" + this.Text + "]");
                return false;
            }
        }
        protected override void SetToolStripButtonBar(CommandBarManager.ActionCommand cmd)
        {
            
        }
        protected override void SetStatusInputControl(System.Windows.Forms.Control container)
        {
            
        }

        protected override bool CancelButtonClick()
        {
            SelectionList.Clear();
            SelectionResult = DialogResult.Cancel;
            this.Close();
            return true;
        }

        protected override bool SaveButtonClick()
        {
            CO_SIGN_LST objSelection;
            List<TIME_CFN> lstCfn = new List<TIME_CFN>();
            TIME_CFN objCfn;

            foreach (var item in grdData.GetCheckedRows())
            {
                SelectionList.Add((CO_SIGN_LST)item.DataRow);
            }

            foreach (var grData in grdData.GetDataRows())
            {
                if (grData.CheckState == Janus.Windows.GridEX.RowCheckState.Checked)
                {
                    objSelection = ((CO_SIGN_LST)grData.DataRow);
                    objCfn = new TIME_CFN();
                    objCfn.Id = objSelection.CODE;
                    objCfn.Text = objSelection.NAME;
                    objCfn.Windex = objSelection.IDX;
                    objCfn.WDataType = objSelection.DATATYPEID;
                    objCfn.FormatString = objSelection.FORMATSTRING;
                    lstCfn.Add(objCfn);
                }
            }

            TimeUtil.UpdateSupportDisplayColumn(lstCfn, FormName);
            SelectionResult = DialogResult.OK;
            this.Close();
            return true;
        }
    }
}
